package com.example.back.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.back.common.Result;
import com.example.back.entity.InStore;
import com.example.back.entity.Store;
import com.example.back.service.CompanyService;
import com.example.back.service.InStoreService;
import com.example.back.service.OutStoreService;
import com.example.back.service.StoreService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Store")
public class StoreController {
    @Resource
    private StoreService storeService;
    @Resource
    private InStoreService inStoreService;
    @Resource
    private OutStoreService outStoreService;
    @Resource
    private CompanyService companyService;

    @GetMapping("/find")
    public Result find(){
        return Result.success(storeService.list());

    }

    @PostMapping("/add")
    public Result add(@RequestBody Store store) {
        try{
            if (companyService.getById(store.getStoreCompany()) == null) {
                return Result.error("添加失败,所属货代公司不存在");
            } else if (storeService.getById(store.getStoreId()) != null ) {
                return Result.error("请勿重复添加仓库");
            } else {
                return Result.success(storeService.save(store),"添加成功");
            }
        }catch(Exception ex){
            System.out.println(storeService.getById(store.getStoreId()));
            return Result.error("添加失败ERROR，违反主键约束，请输入其他ID");
        }


    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Store store) {
        try{
            List<InStore> inStoreList = inStoreService.list();
            for (InStore inStore : inStoreList){
                if (inStore.getInStoreId().equals(store.getStoreId())&&inStore.getRealTon() != 0){
                    return Result.error("删除失败，仓库有货物");
                }
            }
            if (storeService.getById(store.getStoreId()) == null) {
                return Result.error("删除失败，无此仓库");
            }
            else {
                return Result.success(storeService.removeById(store.getStoreId()),"删除成功");
            }
        }catch(Exception ex){
            return Result.error("删除失败ERROR");
        }

    }

    @PostMapping("/modify")
    public Result modify(@RequestBody Store store){
        if (storeService.list().size() == 0 || storeService.getById(store.getStoreId()) == null||companyService.getById(store.getStoreCompany()) == null){
            return Result.error("修改失败");
        }
        else{
            return Result.success(storeService.updateById(store),"修改成功");
        }
    }



    @PostMapping("/get")
    public Result get(@RequestBody Store store) {
        try{
            List<Store> storeList = new ArrayList<>(5);
            if (storeService.getById(store.getStoreId()) != null){
                storeList.add(storeService.getById(store.getStoreId()));
                return Result.success(storeList,"查找成功");
            }
            else {
                return Result.error("查找失败");
            }
        }catch(Exception ex){
            return Result.error("查找失败ERROR");
        }
    }


}
